package cn.yuan.xiaoyu.testmodule.view;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.yuan.xiaoyu.R;

/**
 * Created by yukuoyuan on 2017/9/12.
 */
public class ChoseStatusFrameLayout extends FrameLayout implements View.OnClickListener {
    private Context context;
    private final int VIEW_LOADING_INDEX = 0, VIEW_ERROR_INDEX = 1, VIEW_CONTENT_INDEX = 2;//布局层级
    private OnErrorLayoutButttonClickListener onErrorLayoutButttonClickListener;

    public ChoseStatusFrameLayout(@NonNull Context context) {
        this(context, null);
    }

    public ChoseStatusFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChoseStatusFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
        initData();
    }

    /**
     * 初始化逻辑操作
     */
    private void initData() {
        TextView tv_root_error_refresh_date = (TextView) findViewById(R.id.tv_root_error_refresh_date);
        tv_root_error_refresh_date.setOnClickListener(this);
    }

    /**
     * 初始化视图
     */
    private void initView() {
        /**
         * 设置布局的宽高
         */
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setLayoutParams(layoutParams);
        /**
         * 添加加载进度的布局
         */
        View loadingview = View.inflate(context, R.layout.dialog_iosprogress, null);
        LinearLayout.LayoutParams loadingLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addView(loadingview, VIEW_LOADING_INDEX, loadingLayoutParams);
        /**
         * 添加加载失败的布局
         */
        View errorview = View.inflate(context, R.layout.activity_root_error, null);
        LinearLayout.LayoutParams errorLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addView(errorview, VIEW_ERROR_INDEX, errorLayoutParams);
    }


    /**
     * 布局动画切换
     *
     * @param visible
     */
    private void changeShowAnimation(int childrenIndex, int visible) {
        int animId = 0;
        View view = this.getChildAt(childrenIndex);
        switch (visible) {
            case View.VISIBLE:
                if (view.getVisibility() == View.VISIBLE) {
                    return;
                }
                view.setVisibility(View.VISIBLE);
                animId = android.R.anim.fade_in;
                break;
            case View.GONE:
                if (view.getVisibility() == View.GONE) {
                    return;
                }
                view.setVisibility(View.GONE);
                animId = android.R.anim.fade_out;
                break;
        }
        Animation anim = AnimationUtils.loadAnimation(context, animId);
        anim.setDuration(getResources().getInteger(android.R.integer.config_shortAnimTime));
        view.startAnimation(anim);
    }

    /**
     * 展示内容布局
     */
    public void showContentLayout() {
        changeShowAnimation(VIEW_LOADING_INDEX, GONE);
        changeShowAnimation(VIEW_CONTENT_INDEX, VISIBLE);
        changeShowAnimation(VIEW_ERROR_INDEX, GONE);
    }

    /**
     * 展示加载布局
     */
    public void showLoadingLayout() {
        changeShowAnimation(VIEW_LOADING_INDEX, VISIBLE);
        changeShowAnimation(VIEW_CONTENT_INDEX, GONE);
        changeShowAnimation(VIEW_ERROR_INDEX, GONE);
    }

    /**
     * 展示失败布局
     */
    public void showErrorLayout() {
        changeShowAnimation(VIEW_LOADING_INDEX, GONE);
        changeShowAnimation(VIEW_CONTENT_INDEX, GONE);
        changeShowAnimation(VIEW_ERROR_INDEX, VISIBLE);
    }

    /**
     * 添加内容层级布局
     *
     * @param resource
     */
    public void AddContentLayout(@LayoutRes int resource) {
        /**
         * 添加加载失败的布局
         */
        View errorview = View.inflate(context, resource, null);
        LinearLayout.LayoutParams errorLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addView(errorview, VIEW_CONTENT_INDEX, errorLayoutParams);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_root_error_refresh_date:
                showLoadingLayout();
                if (onErrorLayoutButttonClickListener != null) {
                    onErrorLayoutButttonClickListener.onRefreshDataButtonClick();
                }
                break;
        }
    }

    /******
     *
     *
     * 接口
     */
    public interface OnErrorLayoutButttonClickListener {
        void onRefreshDataButtonClick();
    }

    /**
     * 设置失败布局的按钮的事件的监听
     *
     * @param onErrorLayoutButttonClickListener
     */
    public void setOnErrorLayoutButttonClickListener(OnErrorLayoutButttonClickListener onErrorLayoutButttonClickListener) {
        this.onErrorLayoutButttonClickListener = onErrorLayoutButttonClickListener;
    }
}
